/*
 * @Author: LCL 
 * @Date: 2019-01-11 11:39:20
 * @Desc: 分享全局管理器
 */
import { k } from "./K";
import { LaddGameUtil } from "./LaddGameUtil";



const wx = window['wx']
export class KingShareManager {
    
    private static _instance = null;
    private callBack : Function = null;
    private shareType : number = 0;

    private startTime : number = 0;
    private shareCount : number = 0;
    private endTime : number = 0; 

    private shareContents = [];


    public static  getInstance(): KingShareManager {
        if(!this._instance){
            this._instance = new KingShareManager
            k.storage.setItem("share_starttime",0);
            if( cc.sys.platform == cc.sys.WECHAT_GAME){
                // wx.onShow(this._instance.onShown);
            }

            this._instance.shareContents =[{
                "title":"你能看出几只动物?",
                "imageUrl":"http://cdnwz.tqbapp.com/king_era_share_1.jpg",
            },
            {
                "title":"送你一座矿山，助你登上人生巅峰",
                "imageUrl":"http://cdnwz.tqbapp.com/king_era_share_2.jpg",
            },
            {
                "title":"微软笔试题，了解一下",
                "imageUrl":"http://cdnwz.tqbapp.com/king_era_share_3.jpg",
            },
            {
                "title":"我和你没什么好谈的，除了谈恋爱",
                "imageUrl":"http://cdnwz.tqbapp.com/king_era_share_4.jpg",
            },
            {
                "title":"祖宅拆迁，才知道老宅竟然价值6亿！！",
                "imageUrl":"http://cdnwz.tqbapp.com/king_era_share_5.jpg",
            },
            {
                "title":"出来修仙啊，你睡觉是没有前途的",
                "imageUrl":"http://cdnwz.tqbapp.com/king_era_share_6.jpg",
            },
            {
                "title":"我知道你会算错",
                "imageUrl":"http://cdnwz.tqbapp.com/king_era_share_7.jpg",
            },
            {
                "title":"我在改革开放的道路上",
                "imageUrl":"http://cdnwz.tqbapp.com/king_era_share_8.jpg",
            },
            {
                "title":"我家有矿",
                "imageUrl":"http://cdnwz.tqbapp.com/king_era_share_9.jpg",
            },
        ];

        }
        return this._instance;
    }

    /** 发起分享
     * type  分享类型
     * call 回调函数
     */
    onShare(type:number,call:Function,ctx:any){
        
        this.getShareCount();

        this.callBack = call;
        this.shareType = type;
        this.startTime = new Date().getTime()/1000;
        k.storage.setItem("share_starttime",this.startTime);
        let num = LaddGameUtil.getRandomInt(0,8);
        console.log("随机数:"+num);
        let title = KingShareManager.getInstance().shareContents[num].title;
        let imageUrl = KingShareManager.getInstance().shareContents[num].imageUrl;
        if( cc.sys.platform == cc.sys.WECHAT_GAME){
            wx.shareAppMessage({
                title: title,
                imageUrl:imageUrl,
                // query:`userId=${userServer.id}&serverId=${userServer.serverId}`
            }); 
        }
        else{
            // setTimeout(()=>{
            //     this.onShown("");
            // },3000);
            // wx.showToast({title:"微信小游戏才能分享哦"}); 
        } 
       
    }
    /**  获取卡片带来的参数  query{userId:"",serverId:""}*/
    getOtherUserInfo(){
        let obj =  wx.getLaunchOptionsSync()
        return obj.query
    }

    /** 获得当日邀请总次数   */
    getShareCount(){
        this.shareCount = 0;
        let date = new Date();
        let key:string = "sharecount_" + date.toLocaleDateString();
        this.shareCount =  k.storage.getItem(key);
        if(""+this.shareCount == "true"){
            this.shareCount = 0;
        }
        if(this.shareCount <= 0 || !this.shareCount ){
            this.shareCount = 0;
        }
        console.log("分享次数:"+this.shareCount);
        return  this.shareCount;
    }

    /** 微信回到界面 */
    onShown(res){
        KingShareManager.getInstance().startTime =  k.storage.getItem("share_starttime");
        console.log("回来了");
        console.log("start:"+KingShareManager.getInstance().startTime);
        //如果没有开始事件则 忽略不处理
         if(KingShareManager.getInstance().startTime <= 0){
             return;
         }
         let code:number = 0;
         KingShareManager.getInstance().endTime = new Date().getTime()/1000;
         let diff = KingShareManager.getInstance().endTime - KingShareManager.getInstance().startTime;
         console.log("start:"+KingShareManager.getInstance().startTime + " end:"+KingShareManager.getInstance().endTime + " diff:" +diff);

         //根据间隔时间 和分享次数 判断分享成功、失败
         if(diff <2){
            //  kingToastAwardManager.getInstance().show("请完成分享领取奖励噢");
         }
         else if( diff < 4){
             if(KingShareManager.getInstance().shareCount < 2){
                code = 1;
                // kingToastAwardManager.getInstance().show("分享成功");
                KingShareManager.getInstance().shareCount ++;
             }
             else{
                // kingToastAwardManager.getInstance().show("请分享到群领取奖励哟");
             }
         }
         else if(diff < 7){
            if(KingShareManager.getInstance().shareCount < 5){
                code = 1;
                // kingToastAwardManager.getInstance().show("分享成功");
                KingShareManager.getInstance().shareCount ++;
             }
             else{
                // kingToastAwardManager.getInstance().show("请分享到不同得群领取奖励");
             }
         }
         else{
            code = 1;
            // kingToastAwardManager.getInstance().show("分享成功");
            KingShareManager.getInstance().shareCount ++;
         }

         KingShareManager.getInstance().startTime = 0;


        /** 存储当天分享次数 */
        let date = new Date();
        let key:string = "sharecount_" + date.toLocaleDateString();
        k.storage.setItem(key,KingShareManager.getInstance().shareCount);

         //回调
        let data = {"type":KingShareManager.getInstance().shareType,
        "code":code,
        "shareCount":KingShareManager.getInstance().shareCount}
        KingShareManager.getInstance().callBack(data);

    }
}
window["KingShareManager"] = KingShareManager;


/*
public void TEXT1() 
{
	int textNum = 1;
	if (textNum == 1) 
	{
		textNum = 2;
    }
}
public void TEXT2()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
public void TEXT3()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT4()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT5()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT6()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT8()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT9()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT10()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT11()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT12()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT13()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT14()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT15()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT16()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT17()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
*/